<template>
  <el-dialog
    :destroy-on-close="true"
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="公告类型" prop="notice_cid">
        <el-select v-model="form.notice_cid" placeholder="请选择公告类型">
          <el-option
            v-for="item in list"
            :key="item.id"
            :label="item.cat_title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="公告标题" prop="notice_title">
        <el-input v-model="form.notice_title" />
      </el-form-item>
      <el-form-item label="公告内容" prop="$notice_content">
        <el-input v-model="form.$notice_content" rows="5" type="textarea" />
      </el-form-item>
      <el-form-item label="作者" prop="notice_author">
        <el-input v-model="form.notice_author" />
      </el-form-item>
      <el-form-item label="发布时间" prop="publish_time">
        <el-date-picker
          v-model="form.publish_time"
          placeholder="选择日期时间"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="是否置顶" prop="is_top">
        <el-switch
          v-model="form.is_top"
          active-text="置顶"
          :active-value="1"
          inactive-text="正常"
          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number
          v-model="form.sort"
          :step="1"
          step-strictly
          @change="numberChange"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch
          v-model="form.status"
          active-text="显示"
          :active-value="1"
          inactive-text="隐藏"
          :inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { addNotice, editNotice, getNoticeType } from '@/api/notice'

  export default {
    name: 'NoticeDetail',
    data() {
      return {
        form: {
          notice_title: '',
          $notice_content: '',
          notice_author: '',
          publish_time: '',
          is_top: 0,
          sort: 0,
          status: 1,
        },
        rules: {
          notice_cid: [
            { required: true, trigger: 'blur', message: '请选择公告类型' },
          ],
          notice_title: [
            { required: true, trigger: 'blur', message: '请输入公告标题' },
          ],
          $notice_content: [
            { required: true, trigger: 'blur', message: '请输入公告内容' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        list: [],
      }
    },
    methods: {
      handleCheck(data, check) {
        this.role = check.checkedKeys
      },
      numberChange(value) {
        this.form.sort = value
        this.$forceUpdate()
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
          this.form.$notice_content = row.notice_content
          if (this.form.publish_time == 0) {
            this.form.publish_time = ''
          } else {
            this.form.publish_time = row.publish_time_format
          }
        }
        this.getType()
        this.dialogFormVisible = true
        this.$forceUpdate()
      },

      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      async getType() {
        const { data } = await getNoticeType({})
        this.list = data.items
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            console.log(this.form)
            if (this.title == '添加') {
              const { data } = await addNotice(this.form)
              if (data.is_success) {
                this.$baseMessage(
                  '添加成功',
                  'success',
                  'vab-hey-message-success'
                )
              }
            } else if (this.title == '编辑') {
              const { data } = await editNotice(this.form)
              if (data.is_success) {
                this.$baseMessage(
                  '修改成功',
                  'success',
                  'vab-hey-message-success'
                )
              }
            }
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .vab-tree-border {
    height: 200px;
    padding: $base-padding;
    overflow-y: auto;
    border: 1px solid #dcdfe6;
    border-radius: $base-border-radius;
  }
</style>
